package prefuse.data.parser;

import java.awt.geom.Rectangle2D;
import java.util.regex.Pattern;


//WEENY added
public class RectParser implements DataParser{

	public boolean canParse(String text) {
		return false;
	}

	public boolean compatible(Class type) {
		return Rectangle2D.class.isAssignableFrom(type);
	}

	public static final String X = ":X=";
	public static final String Y = ":Y=";
	public static final String W = ":W=";
	public static final String H = ":H=";
	static Pattern split = Pattern.compile("(?:"+X+")|(?:"+Y+")|(?:"+W+")|(?:"+H+")");
	public String format(Object value) {
		if (value == null) {
			return "";
		}
		Rectangle2D rect = (Rectangle2D) value;
		StringBuffer sb = new StringBuffer();
		sb.append("RECT");
		sb.append(X);sb.append(rect.getX());
		sb.append(Y);sb.append(rect.getY());
		sb.append(W);sb.append(rect.getWidth());
		sb.append(H);sb.append(rect.getHeight());
		return sb.toString();
	}

	public Class getType() {
		return Rectangle2D.Double.class;
	}
	
	public Object parse(String text) throws DataParseException {
		String[] tokens = split.split(text);
		if (tokens.length == 5 && tokens[0].equals("RECT")) {
			double x = Double.parseDouble(tokens[1]);
			double y = Double.parseDouble(tokens[2]);
			double w = Double.parseDouble(tokens[3]);
			double h = Double.parseDouble(tokens[4]);
			return new Rectangle2D.Double(x, y, w, h);
		}
		throw new DataParseException(text+" does not match "+split.pattern());
	}
}
